<template>
	<el-main class="bg edit_wrap comtable_e">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
		<el-row class="row_ce"> 
							<el-col v-if="$check_field('get','shop_name') || $check_field('add','shop_name') || $check_field('set','shop_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="店铺名称" prop="shop_name">
															<el-input id="shop_name" v-model="form['shop_name']" placeholder="请输入店铺名称"
							  v-if="(form['store_information_id'] && $check_field('set','shop_name')) || (!form['store_information_id'] && $check_field('add','shop_name'))" :disabled="disabledObj['shop_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','shop_name')">{{form['shop_name']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','detailed_address') || $check_field('add','detailed_address') || $check_field('set','detailed_address')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="详细地址" prop="detailed_address">
															<el-input id="detailed_address" v-model="form['detailed_address']" placeholder="请输入详细地址"
							  v-if="(form['store_information_id'] && $check_field('set','detailed_address')) || (!form['store_information_id'] && $check_field('add','detailed_address'))" :disabled="disabledObj['detailed_address_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','detailed_address')">{{form['detailed_address']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','business_scope') || $check_field('add','business_scope') || $check_field('set','business_scope')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="经营范围" prop="business_scope">
											<el-select id="business_scope" v-model="business_scope_multiple_value" multiple @change="select_business_scope_multiple"
							   v-if="(form['store_information_id'] && $check_field('set','business_scope')) || (!form['store_information_id'] && $check_field('add','business_scope'))">
												<el-option v-for="o in list_business_scope" :key="o['business_type']" :label="o['business_type']"
								   :value="o['business_type']">
						</el-option>
											</el-select>
					<div v-else-if="$check_field('get','business_scope')">{{form['business_scope']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','business_user') || $check_field('add','business_user') || $check_field('set','business_user')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="商家用户" prop="business_user">
																					<div v-if="user_group !== '管理员'">
							{{ get_user_session_business_user(form['business_user']) }}
							<el-select v-if="(form['store_information_id'] && $check_field('set','business_user')) || (!form['store_information_id'] && $check_field('add','business_user'))" id="business_user" v-model="form['business_user']" :disabled="disabledObj['business_user_isDisabled']">
								<el-option v-for="o in list_user_business_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
							<el-select v-else-if="$check_field('get','business_user')" id="business_user" v-model="form['business_user']" :disabled="true">
								<el-option v-for="o in list_user_business_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
						</div>
						<el-select v-else id="business_user" v-model="form['business_user']" :disabled="disabledObj['business_user_isDisabled']">
							<el-option v-for="o in list_user_business_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
																</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','business_name') || $check_field('add','business_name') || $check_field('set','business_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="商家姓名" prop="business_name">
															<el-input id="business_name" v-model="form['business_name']" placeholder="请输入商家姓名"
							  v-if="(form['store_information_id'] && $check_field('set','business_name')) || (!form['store_information_id'] && $check_field('add','business_name'))" :disabled="disabledObj['business_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','business_name')">{{form['business_name']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','business_phone') || $check_field('add','business_phone') || $check_field('set','business_phone')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="商家电话" prop="business_phone">
															<el-input id="business_phone" v-model="form['business_phone']" placeholder="请输入商家电话"
							  v-if="(form['store_information_id'] && $check_field('set','business_phone')) || (!form['store_information_id'] && $check_field('add','business_phone'))" :disabled="disabledObj['business_phone_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','business_phone')">{{form['business_phone']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','cover_image') || $check_field('add','cover_image') || $check_field('set','cover_image')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="封面图片" prop="cover_image">
											<el-upload :disabled="disabledObj['cover_image_isDisabled']" class="avatar-uploader" drag
						accept="image/gif, image/jpeg, image/png, image/jpg" action="" :http-request="upload_cover_image"
						:show-file-list="false" v-if="(form['store_information_id'] && $check_field('set','cover_image')) || (!form['store_information_id'] && $check_field('add','cover_image'))">
						<img id="cover_image" v-if="form['cover_image']" :src="$fullUrl(form['cover_image'])" class="avatar">
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
					<el-image v-else-if="$check_field('get','cover_image')" style="width: 100px; height: 100px"
						:src="$fullUrl(form['cover_image'])" :preview-src-list="[$fullUrl(form['cover_image'])]">
						<div slot="error" class="image-slot">
							<img src="../../../public/img/error.png" style="width: 90px; height: 90px" />
						</div>
					</el-image>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','shop_introduction') || $check_field('add','shop_introduction') || $check_field('set','shop_introduction')" :xs="24" :sm="24" :lg="24" class="el_form_editor_warp">
							<el-form-item label="店铺简介" prop="shop_introduction">
								<quill-editor v-model.number="form['shop_introduction']"
						v-if="(form['store_information_id'] && $check_field('set','shop_introduction')) || (!form['store_information_id'] && $check_field('add','shop_introduction')) ">
					</quill-editor>
					<div v-else-if="$check_field('get','shop_introduction')" v-html="form['shop_introduction']"></div>
				</el-form-item>
			</el-col>
						
	
	
		
				<el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
			  <el-form-item label="详细地址">
			    <el-input id="mark_address" v-model="form['mark_address']" placeholder="请输入详细地址" clearable v-if="(form['store_information_id}'] && $check_action('/store_information/view', 'set')) || (!form['store_information_id}'] && $check_action('/store_information/view', 'add'))"></el-input>
			    <div v-else-if="$check_action('/store_information/view', 'get')" v-html="form['mark_address']"></div>
			  </el-form-item>
			</el-col>
		
						<el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp" v-if="!form['source_user_id'] && 0 > 0">
			  <el-form-item label="试用预约限次">
				<el-input id="limit_times" v-model="form['trial_appointment_limit_times']" placeholder="试用预约限制次数，0为不限"
						  v-if="$check_option('/store_information/view','can_limits')"
				></el-input>
				<div v-else-if="$check_action('/store_information/view','get')" v-html="form['trial_appointment_limit_times']"></div>
			  </el-form-item>
			</el-col>
					<el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp" v-if="!form['source_user_id'] && 0 > 0">
			  <el-form-item label="维修预约限次">
				<el-input id="limit_times" v-model="form['maintenance_appointment_limit_times']" placeholder="维修预约限制次数，0为不限"
						  v-if="$check_option('/store_information/view','can_limits')"
				></el-input>
				<div v-else-if="$check_action('/store_information/view','get')" v-html="form['maintenance_appointment_limit_times']"></div>
			  </el-form-item>
			</el-col>
			
	
	
		
		
	
		</el-row>
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_btn_warp">
				<el-form-item v-if="$check_action('/store_information/view','set') || $check_action('/store_information/view','add')">
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
				</el-form-item>
				<el-form-item v-else>
					<el-button @click="cancel()">返回</el-button>
				</el-form-item>
			</el-col>

		</el-form>
	</el-main>
</template>

<script>
	import mixin from "@/mixins/page.js";
	import { baiduMap } from "@/utils/baidu_map.js";
	export default {
		mixins: [mixin],
		data() {
			return {
				field: "store_information_id",
				url_add: "~/api/store_information/add?",
				url_set: "~/api/store_information/set?",
				url_get_obj: "~/api/store_information/get_obj?",
				url_upload: "~/api/store_information/upload?",

				query: {
					"store_information_id": 0,
				},

				form: {
								"shop_name":  '', // 店铺名称
										"detailed_address":  '', // 详细地址
										"business_scope":  '', // 经营范围
										"business_user": 0, // 商家用户
										"business_name":  '', // 商家姓名
										"business_phone":  '', // 商家电话
										"cover_image":  '', // 封面图片
										"shop_introduction":  '', // 店铺简介
											"store_information_id": 0, // ID
													"trial_appointment_limit_times": 0, // 试用预约限制次数
							"maintenance_appointment_limit_times": 0, // 维修预约限制次数
											"mark_address": "", // 详细地址
							},
				disabledObj:{
								"shop_name_isDisabled": false,
										"detailed_address_isDisabled": false,
										"business_scope_isDisabled": false,
										"business_user_isDisabled": false,
										"business_name_isDisabled": false,
										"business_phone_isDisabled": false,
										"cover_image_isDisabled": false,
										"shop_introduction_isDisabled": false,
										},

	
			
									business_scope_multiple_value:[],
						// 经营范围选项列表
				list_business_scope: [""],
	
			
					// 用户列表
				list_user_business_user: [],
						// 用户组
				group_user_business_user: "",
					
			
			
			
		
			}
		},
		mounted() {
		  baiduMap().then((BMap) => {
		    this.initMap(BMap);
		  });
		},
		methods: {
			initMap(BMap) {
			  // 创建地图实例
			  var ac = new BMap.Autocomplete({
			    input: "mark_address",
			    location: "中国",
			  });
			  // 监听选中地址事件
			  ac.addEventListener("onconfirm", (e) => {
			    const _value = e.item.value;
			    const value =
			      _value.province +
			      _value.city +
			      _value.district +
			      _value.street +
			      _value.business;
			    this.form.mark_address = value;
			  });
			},
			// 创建地址转经纬度
			getCoordinates(address) {
			  return new Promise((resolve, reject) => {
			    var myGeo = new BMap.Geocoder();
			    myGeo.getPoint(
			      address,
			      function (point) {
			        if (point) {
			          resolve(point);
			        } else {
			          reject("无法获取坐标，请检查地址是否正确");
			        }
			      },
			      "中国"
			    );
			  });
			},

	
	
			
	
			
				/**
			 * 获取经营范围列表
			 */
			
						async get_list_business_scope() {
				var json = await this.$get("~/api/business_type/get_list?");
				if(json.result && json.result.list){
					this.list_business_scope = json.result.list;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
										select_business_scope_multiple(v){
							this.form.business_scope = "";
							if (v && v.length > 0) {
								this.form.business_scope = v.toString();
							}
						},
			
			
	
				/**
			 * 获取商家用户用户列表
			 */
			async get_list_user_business_user() {
                var json = await this.$get("~/api/user/get_list?user_group=商家用户");
                if(json.result && json.result.list){
                    this.list_user_business_user = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
					/**
			 * 获取商家用户用户组
			 */
			async get_group_user_business_user() {
							this.form["business_user"] = this.user.user_id;
							var json = await this.$get("~/api/user_group/get_obj?name=商家用户");
				if(json.result && json.result.obj){
					this.group_user_business_user = json.result.obj;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
			get_user_session_business_user(id){
				var _this = this;
				var user_id = {"user_id":id}
				var url = "~/api/"+_this.group_user_business_user.source_table+"/get_obj?"
				this.$get(url, user_id, function(res) {
					if (res.result && res.result.obj) {
						var arr = []
						for (let key in res.result.obj) {
							arr.push(key)
						}
						var arrForm = []
									for (let key in _this.form) {
							arrForm.push(key)
						}
												_this.form["business_user"] = id
									_this.disabledObj['business_user' + '_isDisabled'] = true
						for (var i=0;i<arr.length;i++){
						  if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
							for (var j = 0; j < arrForm.length; j++) {
							  if (arr[i] === arrForm[j]) {
								if (arr[i] !== "business_user") {
			                      _this.form[arrForm[j]] = res.result.obj[arr[i]]
			                      _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								  break;
								} else {
								  _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								}
							  }
							}
						  }
						}
					}
				});
			},
					get_user_business_user(id){
				var obj = this.list_user_business_user.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},
			
	
			
	
						/**
			 * 上传封面图片
			 * @param {Object} param 图片参数
			 */
			upload_cover_image(param){
									this.uploadFile(param.file, "cover_image");
								},
	
	
			
	
		
			/**
			 * 获取对象之前
			 * @param {Object} param
			 */
			get_obj_before(param) {
				var form = "";
														
				if(this.form && form){
					Object.keys(this.form).forEach(key => {
						Object.keys(form).forEach(dbKey => {
							// if(dbKey === "charging_standard"){
							// 	this.form['charging_rules'] = form[dbKey];
							// 	this.disabledObj['charging_rules_isDisabled'] = true;
							// };
							if(key === dbKey){
								this.disabledObj[key+'_isDisabled'] = true;
								this.form[key] = form[dbKey]
							}
							if(dbKey === "source_table"){
								this.form['source_table'] = form[dbKey];
							}
							if(dbKey === "source_id"){
								this.form['source_id'] = form[dbKey];
							}
							if(dbKey === "source_user_id"){
								this.form['source_user_id'] = form[dbKey];
							}
						})
					})
				}
												$.db.del("form");

				return param;
			},

			/**
			 * 获取对象之后
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json, func){
																				if (this.form.business_scope){
					this.business_scope_multiple_value = this.form.business_scope.split(",")
				}
																													

			},

																																																																				async submit(param, func){
				if (!param) {
					param = this.form;
				}
								if (param["mark_address"]) {
				  const point = await this.getCoordinates(param["mark_address"]);
				  param["mark_lng"] = Number(point.lng.toFixed(8));
				  param["mark_lat"] = Number(point.lat.toFixed(8));
				}
						
				var pm = this.events("submit_before", Object.assign({}, param)) || param;
				var msg = await this.events("submit_check", pm);
				var ret;
				if (msg) {
					this.$toast(msg, 'danger');
				} else {
																																																																			ret = this.events("submit_main", pm, func);
				}
				return ret;
			},
			
			/**
			 * 提交前验证事件
			 * @param {Object} 请求参数
			 * @return {String} 验证成功返回null, 失败返回错误提示
			 */
						submit_check(param) {
					
																																																																																														return null;
			},

			is_view(){
				// var bl = this.user_group == "管理员";
				var bl = false;

				if(!bl){
					bl = this.$check_action('/store_information/table','add');
					console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
				}
				if(!bl){
					bl = this.$check_action('/store_information/table','set');
					console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
				}
				if(!bl){
					bl = this.$check_action('/store_information/view','add');
					console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
				}
				if(!bl){
					bl = this.$check_action('/store_information/view','set');
					console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
				}
				if(!bl){
					bl = this.$check_action('/store_information/view','get');
					console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
				}

				console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

				return bl;
			},
			/**
			 * 上传文件
			 * @param {Object} param
			 */
			uploadimg(param) {
				this.uploadFile(param.file, "avatar");
			},

		},
		created() {
								this.get_list_business_scope();
							this.get_list_user_business_user();
					this.get_group_user_business_user();
													},
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
	
	.img_multiple{
		overflow: hidden;
	}
	.img_multiple .img_block{
		float: left;
		margin-right: 5px;
		margin-bottom: 5px;
		position: relative;
	}
	.img_multiple .img_block img{
		height: 100px;
		width: auto;
	}
	.img_multiple .img_del{
		position: absolute;
		top: 5px;
		right: 5px;
		width: 20px;
		height: 20px;
		background: #0000008a;
		color: #fff;
		line-height: 20px;
		text-align: center;
		border-radius: 100%;
		cursor: pointer;
	}




	
</style>
